package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.system.domain.Material;
import com.ruoyi.system.domain.OrderManage;
import com.ruoyi.system.mapper.MaterialMapper;
import com.ruoyi.system.mapper.OrderManageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ProfitMapper;
import com.ruoyi.system.domain.Profit;
import com.ruoyi.system.service.IProfitService;

/**
 * 成本利润分析Service业务层处理
 * 
 * @author LP
 * @date 2023-04-04
 */
@Service
public class ProfitServiceImpl implements IProfitService 
{
    @Autowired
    private ProfitMapper profitMapper;

    @Autowired
    private MaterialMapper materialMapper;

    @Autowired
    private OrderManageMapper orderManageMapper;
    /**
     * 查询成本利润分析
     * 
     * @param id 成本利润分析主键
     * @return 成本利润分析
     */
    @Override
    public Profit selectProfitById(Long id)
    {
        return profitMapper.selectProfitById(id);
    }

    /**
     * 查询成本利润分析列表
     * 
     * @param profit 成本利润分析
     * @return 成本利润分析
     */
    @Override
    public List<Profit> selectProfitList(Profit profit)
    {
        return profitMapper.selectProfitList(profit);
    }

    /**
     * 新增成本利润分析
     * 
     * @param profit 成本利润分析
     * @return 结果
     */
    @Override
    public int insertProfit(Profit profit)
    {
        return profitMapper.insertProfit(profit);
    }

    /**
     * 修改成本利润分析
     * 
     * @param profit 成本利润分析
     * @return 结果
     */
    @Override
    public int updateProfit(Profit profit)
    {
        return profitMapper.updateProfit(profit);
    }

    /**
     * 批量删除成本利润分析
     * 
     * @param ids 需要删除的成本利润分析主键
     * @return 结果
     */
    @Override
    public int deleteProfitByIds(Long[] ids)
    {
        return profitMapper.deleteProfitByIds(ids);
    }

    /**
     * 删除成本利润分析信息
     * 
     * @param id 成本利润分析主键
     * @return 结果
     */
    @Override
    public int deleteProfitById(Long id)
    {
        return profitMapper.deleteProfitById(id);
    }

    @Override
    public String getProfitCount() {
        // 计算当月材料花费
        List<Material> allMaterialCount = materialMapper.getAllMaterialCount();
        double v = 0.0;
        BigDecimal usedmoney = new BigDecimal(0.0);
        for (Material material : allMaterialCount) {
            int parseInt = Integer.parseInt(material.getQuantity());
            BigDecimal unitPrice = material.getUnitPrice();
            usedmoney = usedmoney.add(unitPrice.multiply(BigDecimal.valueOf(parseInt)));
            v = v + Double.parseDouble(material.getQuantity());
        }
        // 当月材料花费
        List<OrderManage> used = orderManageMapper.getUsed();
        double u = 0.0; BigDecimal sellMoney = BigDecimal.valueOf(0.0);
        for (OrderManage orderManage : used) {
            sellMoney = sellMoney.add(orderManage.getUnitPrice().multiply(BigDecimal.valueOf(Integer.parseInt(orderManage.getQuantity()))));
            u = u + Double.parseDouble(orderManage.getQuantity());
        }
        String earn = String.valueOf(sellMoney.subtract(usedmoney));
        return  earn;
    }

    @Override
    public Double getAllowance() {
        // 计算当月材料总数量
        List<Material> allMaterialCount = materialMapper.getAllMaterialCount();
        double v = 0.0;
        for (Material material : allMaterialCount) {
             v = v + Double.parseDouble(material.getQuantity());
        }
        // 计算当月完成的订单消耗的材料总数
        List<OrderManage> used = orderManageMapper.getUsed();
        double u = 0.0;
        for (OrderManage orderManage : used) {
            u = u + Double.parseDouble(orderManage.getQuantity());
        }
        // 计算余量
        double v1 = v - u;
        return v1;
    }

    @Override
    public int getShipping() {
        List<OrderManage> used = orderManageMapper.getUsed();
        int i = 0;
        for (OrderManage orderManage : used) {
            i = i + Integer.parseInt(orderManage.getQuantity());
        }
        return i;
    }
}
